package com.hospital.yygh.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.hospital.yygh.msm.service.MsmService;
import com.hospital.yygh.vo.msm.MsmVo;
import com.netflix.client.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.rmi.ServerException;
import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 容联云发送手机验证码，弊端，只能是当前手机号，这个是模板，用的时候在注册容联云
     *
     * @param phone
     * @param code
     * @return
     */
    @Override
    public boolean send(String phone, String code) {
        // 生产环境请求地址：app.cloopen.com
        String serverIp = "app.cloopen.com";
        // 端口
        String serverPort = "8883";
        // 主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        String accountSId = "";
        String accountToken = "";
        // 请使用管理控制台中已创建应用的APPID
        String appId = "";
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        // 发送短信到手机号
        String to = phone;
        // 短信模板
        String templateId = "1";
        // 这里模拟一下验证码123456，5分钟内到期
        String[] datas = {code, "5"};
        HashMap<String, Object> result = sdk.sendTemplateSMS(to, templateId, datas);
        if ("000000".equals(result.get("statusCode"))) {
            // 正常返回输出data包体信息（map）
            return true;
        } else {
            // 异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
            return false;
        }
    }

    /**
     * 采用QQ邮箱验证码方式
     *
     * @param phone
     * @param code
     */
    @Override
    @Async
    public boolean sendEmail(String phone, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("法医通项目平台登录验证码");
        message.setText("尊敬的：" + phone + "，您的注册校验验证码为：" + code + "，有效期5分钟");
        message.setTo(phone);
        message.setFrom("1337081497@qq.com");
        try {
            javaMailSender.send(message);
            return true;
        } catch (MailException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * rabbitMQ验证码
     *
     * @param msmVo
     * @return
     */
    @Override
    public boolean sendEmail(MsmVo msmVo) {
        if (!StringUtils.isEmpty(msmVo.getPhone())) {
            boolean isSend = this.sendEmail(msmVo.getPhone(), msmVo.getParam());
            return isSend;
        }
        return false;
    }

    /**
     * QQ邮箱服务整合Rabbitmq发送
     *
     * @param phone
     * @param param
     * @return
     */
    @Async
    public boolean sendEmail(String phone, Map<String, Object> param) {
        // 判断邮箱号是否为空
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        String jsonString = JSONObject.toJSONString(param);
        System.out.println("Rabbitmq发送sendEmail-->" + jsonString);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("法医通项目平台订单详情");
        message.setText("尊敬的：" + phone +
                "，您的订单详情为->"
                + "订单金额：" + param.get("amount")
                + ",姓名：" + param.get("name")
                + ",退号截止时间：" + param.get("quitTime")
                + ",挂号信息：" + param.get("title")
                + ",就诊时间：" + param.get("reserveDate"));
        message.setTo(phone);
        message.setFrom("1337081497@qq.com");
        try {
            javaMailSender.send(message);
            return true;
        } catch (MailException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}
